package com.itheima.store.web.filter;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

import com.itheima.store.domain.User;

/**
 * Servlet Filter implementation class AdminUserFilter
 */
public class AdminUserFilter implements Filter {

    /**
     * Default constructor. 
     */
    public AdminUserFilter() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see Filter#destroy()
	 */
	public void destroy() {
		// TODO Auto-generated method stub
	}

	/**
	 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
	 */
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		//request强转为httprequest
				HttpServletRequest req = (HttpServletRequest) request;
				
				String uri = req.getRequestURI();
				String method = req.getParameter("method");
				StringBuffer url = req.getRequestURL();
				 if(method.contains("loginAdmin")) {//如果包含loginAdmin则表示登录后台放行
					 chain.doFilter(request, response);               
					 
				 }
				 User existUserAdmin = (User) req.getSession().getAttribute("existUserAdmin");
				if(existUserAdmin == null) {
					//不存在 不能让其过去
					req.setAttribute("msg", "您还没有登录，没有权限访问！");
					req.getRequestDispatcher("/admin/index.jsp").forward(req, response);
					return;
				}

		// pass the request along the filter chain
		chain.doFilter(request, response);
	}

	/**
	 * @see Filter#init(FilterConfig)
	 */
	public void init(FilterConfig fConfig) throws ServletException {
		// TODO Auto-generated method stub
	}

}
